package com.ego.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ego.exception.EgoException;
import com.ego.exception.ExceptionEnum;
import com.ego.mapper.BrandMapper;
import com.ego.pojo.Brand;
import com.ego.pojo.vo.ResultVO;
import com.ego.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Ares
 * @date 2020-11-20
 */
@Service
@Slf4j
public class BrandServiceImpl implements BrandService {

    @Resource
    private BrandMapper brandMapper;

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    public ResultVO<Brand> page(Integer pageNo, Integer pageSize, String sortBy, Boolean descending, String key) {
        Page<Brand> brandPage = brandMapper.selectPage(new Page<>(pageNo, pageSize),
                new QueryWrapper<Brand>()
                        .orderBy(StringUtils.isNotEmpty(sortBy), !descending, sortBy)
                        .like(StringUtils.isNotEmpty(key), "name", key)
                        .or()
                        .like(StringUtils.isNotEmpty(key), "letter", key)
        );
        return new ResultVO<>(brandPage.getRecords(),brandPage.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(Long id) {
        Integer count=0;
        count += brandMapper.deleteCategoryBrand(id);
        count += brandMapper.deleteById(id);
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer save(Brand brand, Long[] cids) {
        try {
            Integer count=0;
            count += brandMapper.insert(brand);
            for (Long cid : cids) {
                count += brandMapper.insertCategoryBrand(cid, brand.getId());
            }
            return count;
        } catch (Exception e) {
            EgoException.error(log, ExceptionEnum.BRAND_CREATE_FAILED,e);
            return null;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer update(Brand brand, Long[] cids) {
        Integer count = 0;
        count += brandMapper.updateById(brand);
        count += brandMapper.deleteCategoryBrand(brand.getId());
        for (Long cid : cids) {
            count +=  brandMapper.insertCategoryBrand(cid,brand.getId());
        }
        return count;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Brand> selectBrandsByCid(Long cid) {
        return brandMapper.selectListByCid(cid);
    }

    @Override
    public List<Brand> selectByIds(List<Long> idList) {
        return brandMapper.selectBatchIds(idList);
    }

}
